#include "board.h"
#include <string.h>
#include <stdio.h>
#include "dev_api.h"
#include "lfs_if.h"
#include "ota.h"
#include "com.h"
void jump_to_app(void);
int main(void)
{
  HAL_Init();
  SystemClock_Config();
  iwdg_init();
  gpio_init();
  uart_init();
  lfs_startup();
  ota_update();
  show_version();
  jump_to_app();
  printf("boot startup.\n");
  com_device_init();
  while (1) {
    iwdg_feed();
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10);
    com_service();
  }
  return 0;
}

void jump_to_app(void)
{
  typedef void (*function_t)(void);
  function_t jump_app;
  uint32_t jump_addr;
  uint32_t address;
  address = APP_START_ADDR;
  if (((*(__IO uint32_t*)address) & 0x2FFE0000 ) == 0x20000000) {
    iwdg_feed();
    SysTick->CTRL = 0;
    HAL_NVIC_DisableIRQ(SysTick_IRQn);
    HAL_NVIC_ClearPendingIRQ(SysTick_IRQn);
    uart_deinit();
    jump_addr = *(__IO uint32_t*) (address + 4);
    jump_app = (function_t)jump_addr;
    __set_MSP(*(__IO uint32_t*)address);
    jump_app();
  }
  else {
    printf("Can not find app entry.\n");
    app_version_clean();
  }
}

